package com.liuqi.loggers.core;

import java.util.ArrayList;
import java.util.List;

/**
 * 内置了过滤器
 *
 * @author liuqi
 * @date 2024/6/5 17:19
 **/
public abstract class AbstractAppender extends LifeCycleBase implements Appender {

    protected String name;

    protected List<Filter> filters = new ArrayList<Filter>(1);

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void append(LoggerEvent event) throws LoggingException {
        for (Filter filter : filters) {
            if (filter.decide(event) == FilterReply.DENY) {
                // 过滤日志
                return;
            }
        }

        this.doAppend(event);
    }

    @Override
    public void start() {
        super.start();
    }

    @Override
    public void stop() {
        super.stop();
    }

    protected abstract void doAppend(LoggerEvent event) throws LoggingException;

    public List<Filter> getFilters() {
        return this.filters;
    }

    public void addFilter(Filter filter) {
        this.filters.add(filter);
    }

    public void clearAllFilters() {
        this.filters.clear();
    }

}
